<template>
  <div id="reset-pwd">
    <el-form :rules="rules" :model="form">
      <el-form-item label="输入旧密码">
        <el-input id="oldpwd" label="oldpwd" name="oldpwd" v-model="form.oldpwd"></el-input>
      </el-form-item>
      <el-form-item label="输入新密码" prop="newpwd">
        <el-input id="newpwd" label="newpwd" name="newpwd" v-model="form.newpwd"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {resetPwd} from "@/api/user";
import store from "@/store";

export default {
  name: "ResetPwd",
  data() {
    function validatePassword(rule, value, callback) {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value.length < 6) {
        callback(new Error('密码要大于6'));
      } else {
        callback();
      }
    }

    return {
      form: {
        oldpwd: '',
        newpwd: '',
      },
      rules: {
        newpwd: [
          {validator: validatePassword, trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    async onSubmit() {
      resetPwd(store.state.user.id, this.oldpwd, this.newpwd)
    },
    async cancel() {

    }
  },
}
</script>

<style scoped>

</style>
